
package com.netease.xone.adapter;

import protocol.meta.AppInfo;
import Utils.StackTraceUtil;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.gson.Gson;
import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.itemview.IRenderInfoBase;
import com.netease.xone.itemview.RenderInfoBase;

import db.manager.ManagerAppInfo;

public class AdapterInfo extends CursorAdapter implements LoaderManager.LoaderCallbacks<AppInfo> {

    private static final String TAG = AdapterInfo.class.getSimpleName();
    private static final boolean mUseTask = false;
    private int mLoadId = TAG.hashCode();
    private SparseArray<View> mArray = null;

    public AdapterInfo(Context context, Cursor c) {
        super(context, c, false);
    }

    @Override
    public void bindView(View convertView, Context context, Cursor cursor) {
        int position = cursor.getPosition();
        ViewTag tag = null;
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " position=" + position);
        if (null != convertView && null != convertView.getTag()
                && convertView.getTag() instanceof ViewTag) {
            tag = (ViewTag)convertView.getTag();
            tag.mPostion = position;
        }
        long t1 = 0;
        long t2 = 0;
        t1 = System.currentTimeMillis();
        String value = cursor.getString(ManagerAppInfo.JSON);
        t2 = System.currentTimeMillis();
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " time1=" + (t2 - t1));
        
        if (mUseTask) {
            Bundle bundle = new Bundle();
            bundle.putString("value", value);
            bundle.putInt("position", position);
            if (null == mArray) {
                mArray = new SparseArray<View>();
            }
            mArray.put(value.hashCode(), convertView);
            SherlockFragmentActivity activity = (SherlockFragmentActivity)context;
            activity.getSupportLoaderManager().restartLoader(mLoadId, bundle, this).forceLoad();
        } else {
            AppInfo appInfo = null;
            Gson gson = new Gson();
            appInfo = gson.fromJson(value, AppInfo.class);
            tag.mIRenderInfoBase.renderView(context, appInfo);
            
        }
        t2 = System.currentTimeMillis();
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " time2=" + (t2 - t1));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        int position = cursor.getPosition();
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " position=" + position);
        View view = null;
        view = View.inflate(context, R.layout.item_view_info_base, null);
        RenderInfoBase base = new RenderInfoBase(view);
        ViewTag tag = new ViewTag();
        tag.mPostion = position;
        tag.mIRenderInfoBase = base;
        view.setTag(tag);
        return view;
    }

    @Override
    public Object getItem(int position) {
        Cursor cursor = null;
        String value = null;
        AppInfo appInfo = null;
        cursor = getCursor();
        if (cursor.moveToPosition(position)) {
            value = cursor.getString(ManagerAppInfo.JSON);
            Gson gson = new Gson();
            appInfo = gson.fromJson(value, AppInfo.class);
        }
        
        return appInfo;
    }
    
    public void clear() {
        if (null != mArray) {
            mArray.clear();
        }
        mArray = null;
    }

    @Override
    public Loader<AppInfo> onCreateLoader(int arg0, Bundle bundle) {
        return new ParseLoader(mContext, bundle, mArray);
    }

    @Override
    public void onLoadFinished(Loader<AppInfo> arg0, AppInfo arg1) {
    }

    @Override
    public void onLoaderReset(Loader<AppInfo> arg0) {

    }

    public static class ParseLoader extends AsyncTaskLoader<AppInfo> {

        private int mPosition;
        private String mValue;
        private SparseArray<View> mArray;

        public ParseLoader(Context context, Bundle bundle, SparseArray<View> array) {
            super(context);
            this.mPosition = bundle.getInt("position");
            this.mValue = bundle.getString("value");
            this.mArray = array;
        }

        @Override
        public AppInfo loadInBackground() {
            NTLog.d(TAG, StackTraceUtil.getMethodName());
            long t1 = 0;
            long t2 = 0;
            t1 = System.currentTimeMillis();
            Gson gson = new Gson();
            AppInfo appInfo = null;
            appInfo = gson.fromJson(mValue, AppInfo.class);
            t2 = System.currentTimeMillis();
            NTLog.d(TAG, StackTraceUtil.getMethodName() + " position=" + mPosition + " time1="
                    + (t2 - t1));

            return appInfo;
        }

        @Override
        public void deliverResult(AppInfo data) {
            NTLog.d(TAG, StackTraceUtil.getMethodName());
            long t1 = 0;
            long t2 = 0;
            t1 = System.currentTimeMillis();
            View convertView = mArray.get(mValue.hashCode());
            if (null != convertView && null != convertView.getTag()
                    && convertView.getTag() instanceof ViewTag) {
                ViewTag tag = (ViewTag)convertView.getTag();
                IRenderInfoBase iRenderInfoBase = tag.mIRenderInfoBase;
                iRenderInfoBase.renderView(getContext(), data);
            }
            t2 = System.currentTimeMillis();
            NTLog.d(TAG, StackTraceUtil.getMethodName() + " position=" + mPosition + " time1="
                    + (t2 - t1));
            mValue = null;
            mArray = null;
        }
    }

    public static class ViewTag {
        public IRenderInfoBase mIRenderInfoBase;
        public int mPostion;
    }
}
